package com.example.myapplication_3

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity4 : AppCompatActivity() {
    var seconds = 0
    var running = false
    var wasRunning = false
    private lateinit var handler: Handler

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main4)

        handler = Handler(Looper.getMainLooper())

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        Log.d("life cycle", "onCreate")

        if (savedInstanceState != null) {
            seconds = savedInstanceState.getInt("seconds")
            running = savedInstanceState.getBoolean("running")
            wasRunning = savedInstanceState.getBoolean("wasRunning")
        }

        runTimer()

        // 使用findViewById获取按钮
        val buttonStart = findViewById<android.widget.Button>(R.id.button_start)
        val buttonStop = findViewById<android.widget.Button>(R.id.button_stop)
        val buttonRestart = findViewById<android.widget.Button>(R.id.button_restart)

        buttonStart.setOnClickListener {
            running = true
        }
        buttonStop.setOnClickListener {
            running = false  // 修正：应该是false而不是true
        }
        buttonRestart.setOnClickListener {
            running = true
            seconds = 0
        }
        val buttonBack = findViewById<Button>(R.id.button_back)
        buttonBack.setOnClickListener {
            finish()  // 返回上一个Activity
        }
    }

    fun runTimer() {
        val runnable = object : Runnable {
            override fun run() {
                val hours = seconds / 3600
                val minutes = (seconds % 3600) / 60
                val secs = seconds % 60

                // 使用findViewById获取TextView
                val textView = findViewById<android.widget.TextView>(R.id.textView2_timer)
                textView.text = String.format("%02d:%02d:%02d", hours, minutes, secs)

                if (running) {
                    seconds++
                }
                handler.postDelayed(this, 1000)
            }
        }
        handler.post(runnable)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("life cycle", "onSaveInstanceState")  // 修正日志标签
        outState.putInt("seconds", seconds)
        outState.putBoolean("running", running)
        outState.putBoolean("wasRunning", wasRunning)
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)  // 移除所有回调
        Log.d("life cycle", "onDestroy")
    }

    override fun onStart() {
        super.onStart()
        Log.d("life cycle", "onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("life cycle", "onStop")
    }

    override fun onResume() {
        super.onResume()
        Log.d("life cycle", "onResume")
        if (wasRunning) {
            running = true
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d("life cycle", "onPause")
        wasRunning = running
        running = false
    }
}